package com.guojian.sso.filter.chain;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.sql.DataSource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

/**
 * filter 链条的实现 原理，首先定义一个过滤器filterMain 作为过滤器链的入口，紧接着实现一个 @FilterChain ，
 * 
 * 然后连续不断的调用下一个过滤器 //filterList.get(this.index++).doFilter(request, response,
 * this);
 * 
 * @author GuoJian
 *
 */

@Component
public class FilterFactory implements Filter, InitializingBean {

	@Autowired
	private JdbcTemplate jdbc;

	@Autowired
	private DataSource dataSource;

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

		System.out.println("==============FilterFactory==================");

		List<Filter> filterList = Arrays.asList(new Filter1(), new Filter2());

		FilterChain filterChain = new SsoFilterChain(filterList);

		filterChain.doFilter(request, response);

		chain.doFilter(request, response);

	}

	@Override
	public void afterPropertiesSet() throws Exception {

		System.out.println(dataSource);

		System.out.println("=====================");

		System.out.println(jdbc);

	}

}
